<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="template">
<head th:replace="fragments/tabhead::head"></head>

<body>
<!-- 物料审核 -->
<th:block layout:fragment="content">
<div style="margin-top:20px">
  <form class="layui-form search" id="searchForm">
  <div class="layui-form-item">
     <div class="layui-inline">
      <label class="layui-form-label" style="width:100px">所属公司</label>
      <div class="layui-input-inline">
        <select name="companyId"><option value="1">深圳企智达有限公司</option></select>
      </div>
    </div>
    <div class="layui-inline">
      <label class="layui-form-label" style="width:140px">物料编号/名称 </label>
      <div class="layui-input-inline" style="width:200px">
        <input type="tel" name="codeOrName" class="layui-input" placeholder="请输入物料名称或编码..."/>
      </div>
    </div>
   <div class="layui-inline">
      <label class="layui-form-label" style="width:100px">供应商</label>
      <div class="layui-input-inline" style="width:100px;">
        <select name="supplierId">
        	<option value="" text="请选择供应商信息" selected></option>
        	<option th:each="s : ${supplierList}" th:attr="value=${s.id}" th:text="${s.name}"></option>
        </select>
      </div>
    </div>
    <div class="layui-inline">
      <label class="layui-form-label" style="width:100px">物料类型</label>
      <div class="layui-input-inline" style="width:100px;">
        <select name="materialTypeId">
        	<option value="" text="请选物料类型" selected></option>
        	<option th:each="s : ${materialTypeList}" th:attr="value=${s.id}" th:text="${s.value}"></option>
        </select>
      </div>
    </div>
	
    <div class="layui-inline">
       <div class="layui-input-inline">
        <button class="layui-btn" id="searchBtn" type="button" authcode='900239'>查询</button>
        <button class="layui-btn" id="addBtn" type="button" authcode='900233'>新增</button>
      </div> 
    </div>
    
  </div>
 </form>
</div>

<table class="layui-table"  id="tab" lay-filter="tab"></table>
<script>
var ins,page,trobj,editdialogindex;
$(function(){ 
	layui.use(['table','layer'], function(){
		var table = layui.table;
		ins =table.render({ //其它参数在此省略
		 elem:"#tab",
		 //初始化加载网格
		 url:'/eng/material/review/data',
		 cols:[[
		      {field:'companyName', width:180,title:'所属公司'},
		      {field:'materialCoding', width:100, sort: true,title:'物料编号'},
		      {field:'materialName',width:100,title:'物料名称'},
		      {field:'materialSpec', width:135, sort: true,title:'物料规格'},
		      {field:'materialTypeName', width:80, sort: true,title:'物料类型'},
		      {field:'materialUnitName', width:180, sort: true,title:'计量单位'},
		      {field:'supplierName', width:80,title:'供应商名称'},
		      {field:'isSafied', width:135, sort: true,title:'是否安规' ,templet:'#strueorfalse'},
		      {field:'safetyCertificateStart',width:177,title:'证书生效日期'},
		      {field:'safetyCertificateEnd', width:80, sort: true,title:'证书失效日期'},
		      {field:'isNoUsed', width:80, sort: true,title:'是否停用' ,templet:'#trueorfalse'},
		      {field:'modifyName', width:135, sort: true,title:'修改人'},
		      {field:'modifyTime', width:135, sort: true,title:'修改日期'},
		      {field:'reviewStatus', width:135, sort: true,title:'审核状态',templet:'#status'},
		      {field:'reviewName', width:135, sort: true,title:'审核人'},
		      {field:'reviewTime', width:135, sort: true,title:'审核日期'},
		      {field:'creatorName', width:135, sort: true,title:'创建人'},
		      {field:'ctime', width:135, sort: true,title:'创建时间'},
		      {field:'remark', width:135, sort: true,title:'备注'},
		      {align:'center',fixed:'right',width:300, toolbar:'#toolbar',title:'操作'}
		 ]]
	    ,page: true //是否显示分页
	    ,limits: [10,20,50]
	    ,limit:10 //每页默认显示的数量
		  //method: 'post'
		 ,done:function(res,curr,count){
			 console.log(res);
			 page = curr;
		 	}
		});
		table.on("tool(tab)",function(obj){
			var data = obj.data; //获得当前行数据
			var layEvent = obj.event; //获得 lay-event 对应的值
			trobj = obj;
			var tr = obj.tr; //获得当前行 tr 的DOM对象
			var id = obj.data.id
			var materialName =obj.data.materialName
			if(layEvent=='edit'){
				$.post('/eng/material/review/goforedit?companyid=1&id='+id, {}, function(str){
				editdialogindex = layer.open({
									title:"修改物料审核信息",
								    type: 1, 
								    zIndex:10,
								    maxmin: true,   
								    area: ['1300px', '600px'],
								    content: str //注意，如果str是object，那么需要字符拼接。
								  });
								});
			}else if(layEvent=='del'){
				layer.confirm('确定删除【'+materialName+'】物料审核信息？', {icon: 3, title:'提示'}, function(index){
		    		 //向服务端发送删除指令
		    		$.post("/eng/material/review/deleteReview?companyid=1&id="+id,{},function(result) {
		    			if(result.code == 200) {
		    				obj.del(); //删除对应行（tr）的DOM结构
						    layer.close(index);
						    layer.msg(result.message,{icon: 6});
						    ins.reload({url:'/eng/material/review/data'});
						    return false;
		    			}else{
		    			 layer.msg(result.message,{icon: 0});
		    			 layer.close(index);
		    			 return false;
		    			} 
		    		});
			      });
				return false;
			}else if(layEvent=="review") {
				layer.confirm('确定审核【'+materialName+'】物料审核信息？', {icon: 3, title:'提示'},function(index) {
					if(data.reviewStatus == true) {
			    		layer.msg("物料已审核，不能审核！！", {icon: 4});
			    		layer.close(index);
			    	}else{
			    		$.post('/eng/material/review/audt?id='+data.id,{},function(result) {
			    			if(result.code == 200) {
							    layer.msg(result.message,{icon: 6});
							    ins.reload({
			           	    		url:'/eng/material/review/data'
			           	    	});
							    return false;
			    			}else{
			    			 layer.msg(result.message,{icon: 2});
			    			 return false;
			    			} 
			    		});
			    		layer.close(index);
			    	}
				});
			}else if(layEvent=="celview") {
				layer.confirm('确定弃审【'+materialName+'】?', {icon: 3, title:'提示'},function(index) {
					if(data.reviewStatus == 0) {
			    		layer.msg("单据未审核，不能弃审！！", {icon: 4});
			    		layer.close(index);
			    	}else{
			    		 //向服务端发送删除指令
			    		$.post('/eng/material/review/unaudt?id='+data.id,{},function(result) {
			    			if(result.code == 200) {
			    				obj.update({
			    					reviewStatus:false			    				
			    				});
							    layer.msg(result.message,{icon: 6});
							    ins.reload({
							    	url:'/eng/material/review/data'
			           	    	});
							    return false;
			    			}else{
			    			 layer.msg(result.message,{icon: 2});
			    			 return false;
			    			} 
			    		});
			    		layer.close(index);
			    	}
				});
			}else if(layEvent=="view") {
				$.post('/eng/material/review/view/'+id,{},function(str) {
					editdialogindex = layer.open({
						title:"物料审核查看",
					    type: 1,
					    area: ['800px', '400px'],
					    content: str, //注意，如果str是object，那么需要字符拼接。
					    zIndex:10
					  });
					});
				return false;
		}
		});
		
		//查询按钮事件
		$("#searchBtn").click(function(){
			var table = layui.table;
		    ins.reload({url: '/eng/material/review/search',where:$("#searchForm").serializeJson()})
		});
		
		//新增按钮事件
		$("#addBtn").click(function(){
			var table = layui.table;
			$.post('/eng/material/review/goforadd?companyid=1', {}, function(str){
				//打开  这个异步加载的html页面
				 editdialogindex = layer.open({
							title:"新增物料审核信息",
						    type: 1,
						    zIndex:10,
						    maxmin: true,
						    area: ['1300px', '600px'],
						    content: str //注意，如果str是object，那么需要字符拼接。
						  });
						});
		});
	})
	
	layui.use("laydate",function(){
		var laydate = layui.laydate;
		laydate.render({
			  elem: '#startDate' //指定元素
			});
		laydate.render({
			  elem: '#endDate' //指定元素
			});
	});
	
	

}); 
</script>
<script type="text/html" id="trueorfalse">
	{{#if (d.isNoUsed==false){}}
		<span style="color:red;">否</span>
	{{# }else if(d.isNoUsed==true){}}
		<span style="color:green;">是</span>
	{{# }}}
</script>

<script type="text/html" id="strueorfalse">
	{{#if (d.isSafied==false){}}
		<span style="color:red;">否</span>
	{{# }else if(d.isSafied==true){}}
		<span style="color:green;">是</span>
	{{# }}}
</script>

<script type="text/html" id="status">
	{{#if (d.reviewStatus==false){}}
		<span style="color:red;">未审核</span>
	{{# }else if(d.reviewStatus==true){}}
		<span style="color:green;">已审核</span>
	{{# }}}
</script>
<script type="text/html" id="toolbar">
	<a class="layui-btn  layui-btn-mini" lay-event="view" authcode="900238">详情</a>
	{{#if (d.reviewStatus==false){}}
		<a class="layui-btn layui-btn-mini" lay-event="edit" authcode="900234">修改</a>
		<a class="layui-btn layui-btn-mini" lay-event="review" authcode="900235">审核</a>
		<a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del" authcode="900237">删除</a>
	{{# }else if(d.reviewStatus==true){}}
		<a class="layui-btn layui-btn-mini" lay-event="celview" authcode="900236"">弃审</a>
	{{# }}}
</script>
<!--<div th:replace="/department/model::modelDiv"></div>-->
</th:block>
</body>
</html>